/**
 * Created by fdg296 on 2017/9/29.
 */
$(document).ready(function(){

    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    $.fn.zTree.init($("#treeDemo_other"), setting_other, zNodes);
    $('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/sys-user/get-user-list',
            type:'post',
            data:function(d){
                d.user_name = $('#user_name').val();
                d.true_name = $('#name').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "user_name", "bSortable": false },
            { "data": "true_name", "bSortable": false },
            { "data": "dept_name", "bSortable":false },
            { "data": "mobile", "bSortable":false },
            { "data": "email", "bSortable":false },
            { "data": "last_login_time", "bSortable":false },
            { "data": "last_login_ip", "bSortable":false },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="updateUser('+ full.id +')">修改</a>&nbsp;&nbsp;|&nbsp;&nbsp;' +
                    '<a href="javascript:" onclick="addRole('+ full.id +')">角色</a>';
                }
            },
            {
                "targets": [1],
                "searchable":false
            },
            {
                "targets": [9],
                "data": "use_flg",
                "render": function(data, type, full) {
//                    if(data == 1){
//                        return '<a href="javascript:" onclick="disableClick('+ full.id +')">禁用</a>';
//                    }else{
//                        return '<a href="javascript:" onclick="enableClick('+ full.id +')">启用</a>';
//                    }
                	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
                }
            }
        ],
        order:[[1,'desc']],
        scrollX: true
    });
});

$('#select_button').click(function(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
})

$('#reset_button').click(function(){
    $('#name').val('');
    $('#user_name').val('');
    var table = $('#table_list').DataTable();
    table.ajax.reload();
})
var setting = {
    view: {
        showIcon: false
    },
    data: {
        simpleData: {
            enable: true,
            idKey:"id",
            pIdKey:"pId"
        }
    },
    callback: {
        onClick: zTreeOnClick
    }
};

var setting_other = {
    view: {
        showIcon: false
    },
    data: {
        simpleData: {
            enable: true,
            idKey:"id",
            pIdKey:"pId"
        }
    },
    callback: {
        onClick: zTreeOnClickOther
    }
};

function zTreeOnClick(event, treeId, treeNode) {
    $('#depart_name').val(treeNode.name);
    $('#dept_name').val(treeNode.name);
    $('#dept_id').val(treeNode.id);
    if($('#treeDemo').hasClass('ul_tree_display')){
        $('#treeDemo').removeClass('ul_tree_display');
    }else{
        $('#treeDemo').addClass('ul_tree_display');
    }

};

function zTreeOnClickOther(event, treeId, treeNode) {
    $('#depart_name_other').val(treeNode.name);
    $('#dept_name_other').val(treeNode.name);
    $('#dept_id_other').val(treeNode.id);
    if($('#treeDemo_other').hasClass('ul_tree_display')){
        $('#treeDemo_other').removeClass('ul_tree_display');
    }else{
        $('#treeDemo_other').addClass('ul_tree_display');
    }

};


function showIconForTree(treeId, treeNode) {
    return !treeNode.isParent;
};

$('#depart_name').click(function(){
    if($('#treeDemo').hasClass('ul_tree_display')){
        $('#treeDemo').removeClass('ul_tree_display');
    }else{
        $('#treeDemo').addClass('ul_tree_display');
    }
})

$('#depart_name_other').click(function(){
    if($('#treeDemo_other').hasClass('ul_tree_display')){
        $('#treeDemo_other').removeClass('ul_tree_display');
    }else{
        $('#treeDemo_other').addClass('ul_tree_display');
    }
})

function updateUser(id){
    $('#id').val(id);
    $.post('/sys-user/get-one-user',{id:id},success,'json');
    return false;
    function success(data){
        if(data == null){
            $('#myModal').modal('show');
        }else{
            if(data.code == 500){
                $.jBox.error(data.val, '错误');
            }else{
                $('#true_name').val(data.true_name);
                $('#mobile').val(data.mobile);
                $('#email').val(data.email);
                $('#depart_name').val(data.dept_name);
                $('#dept_id').val(data.dept_id);
                $("input[name='sex'][value='"+data.sex+"']").attr("checked",true);
                $('#dept_name').val(data.dept_name);
                $('#use_flg').val(data.use_flg);

                if(null != data.shop_ids){
                    var sarr = data.shop_ids.split(',');
                    $("#shops").selectpicker('val', sarr);
                }
                
                $('#myModal').modal('show');
            }
        }
    }
}

function addRole(id){
    $('#role-modal').find('input[type=checkbox]').prop('checked',false);
    var role_array = new Array();
    $('#user_id').val(id);
    $.post('/sys-user/get-mapped-role',{user_id:id},success,'json');
    return false;
    function success(data){
        if(data == null){
            $('#role-modal').modal('show');
        }else{
            if(data.code == 500){
                $.jBox.error(data.val, '错误');
            }else{
                var role_string = data.role_id;
                role_array = role_string.split(',');
                $('#role-modal').find('input[type=checkbox]').each(function(){
                    var check_value = $(this).val();
                    var array_num = $.inArray(check_value, role_array);
                    if(eval(array_num) >= 0){
                        $(this).prop('checked',true);
                    }
                })
                $('#role-modal').modal('show');
            }
        }
    }
}

$('.ajax_post_role').click(function(){
    var role_id = $('input[type=checkbox]:checked').val();
    if(role_id == undefined){
        $.jBox.error('请选择角色', '错误');
        return false;
    }
    $.post($('#master_form').attr('action'),$('#master_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { self.location.reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

$('#add_button').click(function(){
    $('#user-modal').modal('show');
})

$('.ajax_post_depart').click(function(){
    var true_name = $('#true_name').val();
    var mobile = $('#mobile').val();
    if(true_name == ''){
        $.jBox.error('请输入姓名', '错误');
        return false;
    }
    if(mobile == ''){
        $.jBox.error('请输入电话', '错误');
        return false;
    }

    $.post($('#user_form').attr('action'),$('#user_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { self.location.reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

$('.ajax_post_master').click(function(){
    var user_name_other = $('#user_name_other').val();
    var user_pwd = $('#user_pwd').val();
    var user_pwd_re = $('#user_pwd_re').val();
    var true_name_add = $('#true_name_other').val();
    var mobile_add = $('#mobile_other').val();
    var shops = $("#shops").val();

    if(user_name_other == ''){
        $.jBox.error('请输入用户名', '错误');
        return false;
    }
    if(user_pwd == ''){
        $.jBox.error('请输入密码', '错误');
        return false;
    }
    if(user_pwd != user_pwd_re){
        $.jBox.error('两次输入密码不一致', '错误');
        return false;
    }
    if(true_name_add == ''){
        $.jBox.error('请输入姓名', '错误');
        return false;
    }
    if(mobile_add == ''){
        $.jBox.error('请输入电话', '错误');
        return false;
    }

    $.post($('#user_info_form').attr('action'),$('#user_info_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { self.location.reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

//禁用
function disableClick(id){
    $.jBox.confirm("确定禁用吗？", "提示", function(v){
        if (v == 'ok'){
            $.post('/sys-user/disable-user',{id:id},success,'json');
            return false;
            function success(data){
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { self.location.reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
            $.jBox.close();
        }
    });
}

//启用
function enableClick(id){
    $.jBox.confirm("确定启用吗？", "提示", function(v){
        if (v == 'ok'){
            $.post('/sys-user/enable-user',{id:id},success,'json');
            return false;
            function success(data){
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { self.location.reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
            $.jBox.close();
        }
    });
}

$('#table_list').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	if(use_flg == '1'){
		enableClick(id);
	}else{
		disableClick(id);
	}
});